//
//  EstateFeeListViewController.h
//  daogouApp
//
//  Created by panquanReabam.com on 15/7/9.
//  Copyright (c) 2015年 jiezsoft.com. All rights reserved.

#import "FeeDetailViewController.h"

#import "HAODEsingleton.h"
#import "UIViewExt.h"
#import "RDVTabBarController.h"
#import "HttpRequest.h"
#import "JSONKit.h"
#import "UIButton+WebCache.h"
#import "daogouview.h"
#import "NoticeDetailViewController.h"
#import "TheLoginViewController.h"
#import "MJRefresh.h"
#import "FeeToPaViewController.h"
#import "feeview.h"
@interface FeeDetailViewController ()<MBProgressHUDDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UITextFieldDelegate>


@end

@implementation FeeDetailViewController

{
    UISearchBar *search;
    HAODEsingleton* commdity;
    MJRefreshHeaderView* _header;
    MJRefreshFooterView* _footer;
    NSMutableArray*feeNo;
    NSMutableArray*feeDate;
    NSMutableArray*feeType;
    NSMutableArray*fid;
    NSMutableArray*totalMoney;
    NSMutableArray*couponMoney;
    NSMutableArray*payDate;
    NSMutableArray* payStatus;
    NSMutableArray* payStatusName;
    NSMutableArray* feeTypeName;
    NSMutableArray* unitName;
    NSMutableArray* unitPrice;
    NSMutableArray* quantity;
    NSMutableArray*  groupName;
    int pageNumber;
    daogouview*daogouviews;
    UILabel* noLabel;
    NSMutableArray *allComment;
    NSMutableArray *arr1;
    UIScrollView*scroll;
    feeview*feeviews;
    NSDictionary *jsonData;
    

}
@synthesize alt = alt;


#pragma mark - TextField delegate


-(void)BackAction:(UIButton *)sender{
    
    
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (void)viewDidDisappear:(BOOL)animated

{
    
    [super viewDidDisappear:animated];
    
    
    
    self.navigationController.delegate = nil;
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [[self rdv_tabBarController] setTabBarHidden:YES];
    self.navigationController.navigationBarHidden = NO;
        self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    
       UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.font=[UIFont systemFontOfSize:13];
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.navigationController.delegate = self;
    
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
    
    [Global showIndeterminateHud:@"获取数据，请稍后.." delay:30 view:self.view];
    [self add];
}
-(void)add{
    pageNumber=1;
    fid = [[NSMutableArray alloc] init];
    feeNo = [[NSMutableArray alloc] init];
    feeDate = [[NSMutableArray alloc] init];
    totalMoney= [[NSMutableArray alloc] init];
    couponMoney=[[NSMutableArray alloc] init];
    payDate = [[NSMutableArray alloc] init];
    payStatus = [[NSMutableArray alloc] init];
    payStatusName = [[NSMutableArray alloc] init];
    feeTypeName= [[NSMutableArray alloc] init];
    unitName = [[NSMutableArray alloc] init];
    unitPrice = [[NSMutableArray alloc] init];
    quantity= [[NSMutableArray alloc] init];
    feeType= [[NSMutableArray alloc] init];
    groupName= [[NSMutableArray alloc] init];
    
    
    self.title=@"物业费详情";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor blackColor],
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
    
    
    commdity=[HAODEsingleton shared];
    
    
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    if (Token ==nil) {
        Token=@"dd";
    }
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    [param setValue:commdity.orderId forKey:@"feeId"];
    
    NSLog(@"====%@",param);
    [HttpRequest requestData:@"/Estate/Fee/Detail" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            
            NSLog(@"33333333====%@",severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                
                NSMutableArray* arrr=[[NSMutableArray alloc]init];
                
                
                arrr=[severDataDict.dataDict objectForKey:@"DataLine"];
                jsonData=severDataDict.dataDict;
                
                
                scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, self.navigationController.navigationBar.bottom, ScreenWidth, ScreenHeight-self.navigationController.navigationBar.bottom-self.tabBarController.tabBar.height)];
                scroll.backgroundColor=zuiqianhuicolor;
                [self.view addSubview:scroll];
                
                NSString*GoldPrice=[NSString stringWithFormat:@"%@",[severDataDict.dataDict objectForKey:@"totalMoney"]];
                NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
                [numberFormatter setPositiveFormat:@"###,##0.00;"];
                NSString *formattedNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:GoldPrice.floatValue]];
                
                for (int d = 0; d<1; d++) {
                    
                    
                    UIView *topView =[self addStoreTopView:CGRectMake(0, 210*d , ScreenWidth, 350) payStatus:[NSString stringWithFormat:@"%@",[jsonData valueForKey:@"payStatus"]] totalMoney:[NSString stringWithFormat:@"%@",formattedNumberString] feeDate:[NSString stringWithFormat:@"%@",[jsonData valueForKey:@"feeDate"]] number:d];
                    
                    
                    
                    [scroll addSubview:topView];
                    
                    for (int i = 0; i < [[jsonData objectForKey:@"items"] count]; i++) {
                        
                        
                        feeviews = [[feeview alloc]initWithFrame:CGRectMake(0,90+25*i ,ScreenWidth, 15*i)];
                        feeviews.backgroundColor=[UIColor clearColor];
                        NSDictionary * dic = [[jsonData objectForKey:@"items"] objectAtIndex:i];//商品信息
                        
                        feeviews.feeType.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"feeTypeName"]];
                        feeviews.feeType.textColor=shenhuicolor;
                        double d;
                        double d1;
                        d= [[dic objectForKey:@"unitPrice"]doubleValue];
                        d1 = [[dic objectForKey:@"quantity"]doubleValue];
                        
                        feeviews.feeTypeName.text = [NSString stringWithFormat:@"%@ 元/%@ x %@ %@ =%.2f",[dic objectForKey:@"unitPrice"],[dic objectForKey:@"unitName"],[dic objectForKey:@"quantity"],[dic objectForKey:@"unitName"],  d*d1 ];
                        feeviews.feeTypeName.textColor=[UIColor blackColor];
                        
                        [topView addSubview:feeviews];
                        
                        
                    }
                    
                    
                    
                    
                }
                
                
                
            }else{
                [Global hideProgressHUDInternal:self.view];
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                // [Global showTextHud:requestResultStr view:self.view];
                [Global showTextHudSub:requestResultStr view:self.view];
                
                
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                    
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    [Global showTextHud:requestResultStr view:self.view];
                    
                    TheLoginViewController*vc1=[[TheLoginViewController alloc] init];
                    
                    [self.navigationController pushViewController:vc1 animated:YES];
                    
                    
                    
                }
            }
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            
            [Global showTextHudSub:@"连接失败" view:self.view];
        }
        
    }];
    
    
}


- (UIView *)addStoreTopView:(CGRect)frame payStatus:(NSString *)payStatusss totalMoney:(NSString *)totalMoneys feeDate:(NSString *)feeDatess number:(NSInteger)number {
    
    
    UIView *viewTop= [[UIView alloc]initWithFrame:frame];
    viewTop.backgroundColor = [UIColor whiteColor];
    UIImageView *_xianview = [[UIImageView alloc]initWithFrame:CGRectMake(15,60, ScreenWidth-30, 0.5)];
    _xianview.image=[UIImage imageNamed:@"hengxian"];
    [viewTop addSubview:_xianview];
    
    UIView*diview=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 10)];
    diview.backgroundColor=zuiqianhuicolor;
    [viewTop addSubview:diview];
    UILabel *line911=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0.35)];
    line911.backgroundColor=qianhuicolor;
    [diview addSubview:line911];
    UILabel *line91=[[UILabel alloc] initWithFrame:CGRectMake(0, 10, ScreenWidth, 0.35)];
    line91.backgroundColor=qianhuicolor;
    [diview addSubview:line91];
    
    UILabel*feeDates = [[UILabel alloc]init];
    feeDates. textColor=[UIColor blackColor];
    [feeDates setFrame:CGRectMake(20, 20, 140, 30)];
    feeDates .text=[NSString stringWithFormat:@"%@物业费", feeDatess];
    feeDates.font =[UIFont systemFontOfSize:16];
    [viewTop addSubview:feeDates];
    
    
    UIButton *button_name = [[UIButton alloc]init];
    
    [button_name setFrame:CGRectMake(ScreenWidth-90, 20, 70, 30)];
    button_name.tag = number;
    [button_name.layer setCornerRadius:3.0];
    [button_name.layer setBorderWidth:0];
    
    NSLog(@"%@",payStatusss);
    
    if ([payStatusss isEqualToString:@"N"]) {
        [button_name addTarget:self action:@selector(zhifu:) forControlEvents:UIControlEventTouchUpInside];
        button_name.backgroundColor=hongsecolor;
        [button_name setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
           [button_name setTitle:@"去缴费" forState:UIControlStateNormal];
    }else{
        
        [button_name setFrame:CGRectMake(ScreenWidth-170, 20, 150, 30)];
        [button_name setTitleColor:luseColor forState:UIControlStateNormal];
        button_name.titleLabel.textAlignment=UIControlContentHorizontalAlignmentRight;
        
        NSRange range={0,10};
        NSString* string =[[jsonData objectForKey:@"payDate"] substringWithRange:range];
        [button_name setTitle:[NSString stringWithFormat:@"缴费日期 %@", string] forState:UIControlStateNormal];
        
    }
    
 
    button_name.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    [viewTop addSubview:button_name];
    
    UILabel *heji = [[UILabel alloc]init];
    heji .textColor=shenhuicolor ;
    [heji setFrame:CGRectMake(20, 225, 65, 16)];
    heji .text= @"合计" ;
    heji.font = [UIFont systemFontOfSize:16];
    [viewTop addSubview:heji];
    NSString*GoldPrice=[NSString stringWithFormat:@"%@",totalMoneys];
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setPositiveFormat:@"###,##0.00;"];
    NSString *formattedNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:GoldPrice.floatValue]];
    UILabel *hejiduoshao =[[UILabel alloc]init];
    hejiduoshao.textColor=shenhuicolor;
    [hejiduoshao setFrame:CGRectMake(ScreenWidth-200, 225, 180, 16)];
    hejiduoshao.text= [NSString stringWithFormat:@"¥ %@" ,formattedNumberString];
    hejiduoshao.font = [UIFont systemFontOfSize:16];
    hejiduoshao.textAlignment=NSTextAlignmentRight;
    [viewTop addSubview:hejiduoshao];


    
   
    if ([payStatusss isEqualToString:@"去缴费"]) {
        
    }else{
    
    
    
    UILabel *youhuijine = [[UILabel alloc]init];
    youhuijine .textColor=hongsecolor ;
    [youhuijine setFrame:CGRectMake(20, 515/2, 65, 45)];
    youhuijine .text= @"优惠金额" ;
    youhuijine.font = [UIFont systemFontOfSize:16];
    [viewTop addSubview:youhuijine];
    
    NSString*GoldPrice1=[NSString stringWithFormat:@"%@",[jsonData objectForKey:@"couponMoney"]];
    NSNumberFormatter *numberFormatter1 = [[NSNumberFormatter alloc] init];
    [numberFormatter1 setPositiveFormat:@"###,##0.00;"];
    NSString *formattedNumberString1 = [numberFormatter1 stringFromNumber:[NSNumber numberWithDouble:GoldPrice1.floatValue]];
    UILabel *youhuijine1 =[[UILabel alloc]init];
    youhuijine1.textColor=hongsecolor;
    [youhuijine1 setFrame:CGRectMake(ScreenWidth-200,515/2, 180, 45)];
    youhuijine1.text= [NSString stringWithFormat:@"¥ %@" ,formattedNumberString1];
    youhuijine1.font = [UIFont systemFontOfSize:16];
    youhuijine1.textAlignment=NSTextAlignmentRight;
    [viewTop addSubview:youhuijine1];
    UIImageView *_xianview1 = [[UIImageView alloc]initWithFrame:CGRectMake(15,610/2, ScreenWidth-30, 0.5)];
    _xianview1.image=[UIImage imageNamed:@"hengxian"];
    [viewTop addSubview:_xianview1];
    UILabel *shifu = [[UILabel alloc]init];
    shifu .textColor=shenhuicolor ;
    [shifu setFrame:CGRectMake(20, 610/2, 65, 45)];
    shifu .text= @"实付金额" ;
    shifu.font = [UIFont systemFontOfSize:16];
    [viewTop addSubview:shifu];
    NSString*GoldPrice2=[NSString stringWithFormat:@"%@",totalMoneys];
    NSNumberFormatter *numberFormatter2 = [[NSNumberFormatter alloc] init];
    [numberFormatter2 setPositiveFormat:@"###,##0.00;"];
    NSString *formattedNumberString2 = [numberFormatter2 stringFromNumber:[NSNumber numberWithDouble:GoldPrice2.floatValue]];
    UILabel *shifu1 =[[UILabel alloc]init];
    shifu1.textColor=[UIColor blackColor];
    [shifu1 setFrame:CGRectMake(ScreenWidth-200,610/2, 180, 45)];
    shifu1.text= [NSString stringWithFormat:@"¥ %@" ,formattedNumberString2];
    shifu1.font = [UIFont systemFontOfSize:16];
    shifu1.textAlignment=NSTextAlignmentRight;
    [viewTop addSubview:shifu1];

    
    }
    
    
    
    
    
    
    
    UILabel *line1=[[UILabel alloc] initWithFrame:CGRectMake(0, 350-0.35, ScreenWidth, 0.35)];
    line1.backgroundColor=qianhuicolor;
    [viewTop addSubview:line1];
    
    UILabel*line2=[[UILabel alloc]initWithFrame:CGRectMake(15, 520/2, ScreenWidth-30, 0.35)];
    line2.backgroundColor=qianhuicolor;
    [viewTop addSubview:line2];
    UILabel*line3=[[UILabel alloc]initWithFrame:CGRectMake(15, 210-0.35, ScreenWidth-30, 0.35)];
    line3.backgroundColor=qianhuicolor;
    [viewTop addSubview:line3];

    
    
    
    return viewTop;
}

- (void)zhifu:(UIButton *)button {
    
    
    //    NSMutableArray *array = [NSMutableArray array];
    //    for (NSDictionary *dic in [[[[jsonData objectForKey:@"DataLine"] allValues] objectAtIndex:button.tag] allValues]) {
    //        [array addObject:[dic objectForKey:@"cart_id"]];
    //
    //    }
    //从数组合并元素到字符串- componentsJoinedByString:
    
    //    NSString *string1 = [array componentsJoinedByString:@","];
    
   
        FeeToPaViewController* store=[[FeeToPaViewController alloc]init];
    
        [self.navigationController pushViewController:store animated:YES];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定哦 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}



- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}
@end
